package com.yipao.db.service.devinfo;

import com.github.pagehelper.PageHelper;
import com.yipao.db.dao.dev.DevInfoMapper;
import com.yipao.db.domain.devinfo.DevInfo;
import com.yipao.db.domain.devinfo.DevInfoExample;
import com.yipao.db.domain.excel.ExportDevInfoVo;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author voyboy
 */
@Service
public class YipaoDevInfoService {

    @Resource
    private DevInfoMapper yipaoDevInfoMapper;

    /**
     * 查询所有
     *
     * @param page  第几页
     * @param limit 一页显示多少
     * @param sort
     * @param order
     * @return
     */
    public List<DevInfo> findAll(String devCode, String title, String employee,
                                 Integer page, Integer limit, String sort, String order) {
        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(devCode)) {
            criteria.andDevCodeEqualTo(devCode);
        }
        if (!StringUtils.isEmpty(title)) {
            criteria.andScenesLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(employee)) {
            criteria.andEmployeeLike("%" + employee + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        criteria.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        PageHelper.startPage(page, limit);
        return yipaoDevInfoMapper.selectByExample(example);
    }


    /**
     * 新增
     *
     * @param
     */
    public void insert(DevInfo devInfo) {
        Integer maxDevId = yipaoDevInfoMapper.getMaxDevCode();
        devInfo.setAddTime(LocalDateTime.now() + "");
        devInfo.setUpdateTime(LocalDateTime.now() + "");
        int i = maxDevId + 1;
        devInfo.setDevCode(String.valueOf(i));
        yipaoDevInfoMapper.insert(devInfo);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public DevInfo selectByPrimaryKey(Integer id) {

        return yipaoDevInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新
     *
     * @param devInfo
     */
    public void update(DevInfo devInfo) {
        devInfo.setUpdateTime(LocalDateTime.now() + "");
        yipaoDevInfoMapper.updateByPrimaryKeySelective(devInfo);
    }

    /**
     * 根据ID删除
     *
     * @param id
     */
    public void delete(Integer id) {
        DevInfo devInfo = yipaoDevInfoMapper.selectByPrimaryKey(id);
        devInfo.setDevOnline("1");
        yipaoDevInfoMapper.updateByPrimaryKeySelective(devInfo);
    }

    /**
     * 多条记录删除
     *
     * @param ids
     */
    public void deleteIds(Integer[] ids) {
        for (Integer id : ids) {
            yipaoDevInfoMapper.updateByPrimaryKeySelective(new DevInfo(id, DeleteStatusConstant.IS_DELETED));
        }
    }

    /**
     * 根据条件查询
     *
     * @param
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<DevInfo> selectByExample(String devId, String scenes, String employee,
                                         Integer page, Integer limit, String sort, String order) {

        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(String.valueOf(devId))) {
            criteria.andDevCodeEqualTo(devId);
        }
        if (!StringUtils.isEmpty(scenes)) {
            criteria.andScenesLike(scenes);
        }
        if (!StringUtils.isEmpty(employee)) {
            criteria.andEmployeeLike("%" + employee + "%");
        }
        PageHelper.startPage(page, limit);
        return yipaoDevInfoMapper.selectByExample(example);

    }

    /**
     * 获取所有状态码(去重)
     *
     * @return
     */
    public List<Integer> getAllStatusCode() {
        return yipaoDevInfoMapper.getAllStatusCode();
    }

    /**
     * 获取所有故障状态(去重)
     *
     * @return
     */
    public List<Integer> getAllDevStatus() {
        return yipaoDevInfoMapper.getAllDevStatus();
    }

    /**
     * 根据时间查询
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<DevInfo> getByTime(LocalDateTime startTime, LocalDateTime endTime) {
        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();
        if (startTime != null) {
            criteria.andHappenedTimeGreaterThan(startTime + "");
        }
        if (endTime != null) {
            criteria.andHappenedTimeLessThan(endTime + "");
        }
        return yipaoDevInfoMapper.selectByExample(example);
    }

    /**
     * 根据故障码搜索
     *
     * @param statusCode
     * @return
     */
    public List<DevInfo> findDevByStatusCode(String statusCode) {

        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();
        criteria.andStatusCodeEqualTo(statusCode);


        return yipaoDevInfoMapper.selectByExample(example);
    }

    public List<ExportDevInfoVo> findAllExcel() {
        return yipaoDevInfoMapper.findAllExcel();
    }
}
